package com.tommy.ddp.manager
{
	import com.tommy.ddp.box.DdpBox;
	import com.tommy.ddp.definition.BoxExeType;
	import com.tommy.ddp.definition.GameStatus;
	import com.tommy.ddp.disposeHandler.AllXAxisDisposeHandler;
	import com.tommy.ddp.disposeHandler.AllYAxisDisposeHandler;
	import com.tommy.ddp.disposeHandler.BoomCloneDisposeHandler;
	import com.tommy.ddp.disposeHandler.BoomDisposeHandler;
	import com.tommy.ddp.disposeHandler.EmptyDisposeHandler;
	import com.tommy.ddp.disposeHandler.IDisposeHandler;
	import com.tommy.ddp.disposeHandler.SuperBoomDisposeHandler;
	import com.tommy.ddp.events.DisposeVoHandlerEvent;
	import com.tommy.ddp.swapHandle.DisposeVoHandler;
	import com.tommy.ddp.swapHandle.SubDisposeVo;

	/**
	 * 所有的格子消除时的统一入口管理
	 * @author hongxuan
	 * 
	 */
	public class DisposeBoxManager
	{
		private static var boomCloneList:Vector.<DdpBox> = new Vector.<DdpBox>();
		
		public function DisposeBoxManager()
		{
		}
		/**
		 * 记录生成的小炸弹，在掉落完以后处理
		 * @param box
		 * 
		 */		
		public static function addBoomClone(box:DdpBox):void
		{
			if(boomCloneList.indexOf(box) == -1)
				boomCloneList.push(box);
		}
		
		/**
		 * 炸弹数量
		 * @return 
		 * 
		 */		
		public static function get boomNum():uint
		{
			return boomCloneList.length;
		}
		
		
		public static function clearBoom():void
		{
			trace("小炸弹清除中…………",boomNum);
			var vo:SubDisposeVo = new SubDisposeVo();
			vo.vect = boomCloneList;
			vo.type = BoxExeType.BOOM_CLONE;
			
			var h:DisposeVoHandler = new DisposeVoHandler();
			h.addEventListener(DisposeVoHandlerEvent.COMPLETE,onComplete);
			h.executeSub(vo);
			
			boomCloneList = new Vector.<DdpBox>();
		}
		private static function onComplete(event:DisposeVoHandlerEvent):void
		{
			trace("小炸弹 or 特殊交换清除完毕，再次填充……");
			
			var h:DisposeVoHandler = event.currentTarget as DisposeVoHandler;
			h.removeEventListener(DisposeVoHandlerEvent.COMPLETE,onComplete);
			h.dispose();
			
			GameStatusManager.changeStatus(GameStatus.BEGIN_FILL);
			//FillUpManager.fillUp();
		}
		/**
		 * 消除某个棋子
		 * @param ddpBox
		 * 
		 */		
		public static function addDisposeBox(ddpBox:DdpBox):Vector.<DdpBox>
		{
			//处理消除的格子置一个状态
			ddpBox.isInDisposeList = true;
			var handler:IDisposeHandler = getDisposeHandler(ddpBox);
			//trace("disposeHandler--->",handler)
			return handler.dispose(ddpBox);
		}
		
		
		private static function getDisposeHandler(box:DdpBox):IDisposeHandler
		{
			var handler:IDisposeHandler;
			
			if(box.exeType == BoxExeType.X_AXIS_ALL)
				handler = new AllXAxisDisposeHandler();
			else if(box.exeType == BoxExeType.Y_AXIS_ALL)
				handler = new AllYAxisDisposeHandler();
			else if(box.exeType == BoxExeType.BOOM)
				handler = new BoomDisposeHandler();
			else if(box.exeType == BoxExeType.BOOM_CLONE)
				handler = new BoomCloneDisposeHandler();
			else if(box.exeType == BoxExeType.SUPER_BOOM)
				handler = new SuperBoomDisposeHandler();
			else
				handler = new EmptyDisposeHandler(); 
			
			return handler;
		}
		
		
		
		public static function disposeSubVo(vo:SubDisposeVo):void
		{
			var h:DisposeVoHandler = new DisposeVoHandler();
			h.addEventListener(DisposeVoHandlerEvent.COMPLETE,onComplete);
			h.executeSub(vo);
		}
	}
}